package com.sxran.im.handler;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sxran.im.controller.ImController;
import com.sxran.im.dao.ImGroupInviteMapper;
import com.sxran.im.dao.ImUserGroupMapper;
import com.sxran.im.info.ImFriendInvite;
import com.sxran.im.info.ImGroupInvite;
import com.sxran.im.service.FriendInviteService;
import com.sxran.im.service.NotificationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Slf4j
@Service
public class RefuseGroupApplicationCmdHandler extends AbstractCmdHandler<ImGroupInvite>{
    @Resource
    private ImGroupInviteMapper imGroupInviteMapper;
    @Resource
    private ImUserGroupMapper imUserGroupMapper;
    @Resource
    protected FriendInviteService friendInviteService;
    @Resource
    protected NotificationService notificationService;
    @Override
    public Object doBeanService(ImGroupInvite json) {
        // 修改申请
        updateState(json, -100);
        return json;
    }

    protected void updateState(ImGroupInvite json, int refuseState) {
        json.setState(refuseState);
        String curUserId = ImController.getCurUserId();
//        json.setUserId(curUserId);
        LambdaQueryWrapper<ImGroupInvite> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ImGroupInvite::getGroupId,json.getGroupId());
        String userId = json.getUserId();
        queryWrapper.eq(ImGroupInvite::getUserId, userId);
        imGroupInviteMapper.update(json,queryWrapper);

        // 修改通知
        // TODO 群聊管理员所有的通知消息都修改对应的状态。
        notificationService.updateGroupApplicationState(userId,curUserId, refuseState , json.getMessage());
    }

}
